1 module hip.filesystem.systems.browser;
2 
3 version(WebAssembly):
4 
5 /**
6 *   directories.json is an auto generated file which saves a list of all directories for your game assets.
7 *   With that, it is possible to reproduce some commands such as exists or isDir.
8 *   It is also possible to get the file size upfront.
9 */
10 immutable string directories = import("directories.json");
11 
12 import hip.api.filesystem.hipfs;
13 import hip.filesystem.hipfs;
14 
15 version(WebAssembly):
16 import hip.wasm;
17 
18 
19 private extern(C) void WasmRead(JSStringType str,
20     JSDelegateType!(void delegate(ubyte[])) onSuccess, 
21     JSDelegateType!(void delegate(string)) onError
22 );
23 
24 class HipBrowserFileSystemInteraction : IHipFileSystemInteraction
25 {
26     import hip.data.json;
27     JSONValue dirsJson;
28     this()
29     {
30         dirsJson = parseJSON(directories);
31         if(dirsJson.hasErrorOccurred)
32         {
33             import hip.error.handler;
34             ErrorHandler.assertExit(false, "Could not parse directories.json, required for BrowserFS. Got `"~directories~"`\n\t Error: "~dirsJson.error);
35         }
36     }
37     
38     bool read(string path, FileReadResult delegate(ubyte[] data) onSuccess, void delegate(string err = "Corrupted File") onError)
39     {
40         JSONValue dummy = void;
41         import hip.console.log;
42         if(!getFromPath(path, dummy))
43         {
44             hiplog("Browser could not read ", path);
45             return false;
46         }
47         hiplog("Browser read start on ", path);
48 
49         WasmRead(JSString(path).tupleof, sendJSDelegate!((ubyte[] wasmBin, WasmParametersMemory memory)
50         {
51             onSuccess(wasmBin);
52             ///TODO: Fix to free WasmParametersMemory
53             // if(onSuccess(wasmBin) == FileReadResult.free)
54             // {
55             //     import core.memory;
56             //     GC.free(memory.ptr);
57             // }
58 
59         }).tupleof, sendJSDelegate!(onError).tupleof);
60         
61         return true;
62     }
63     bool write(string path, const(void)[] data){return false;}
64 
65     private bool getFromPath(string path, out JSONValue output)
66     {
67         import hip.util.path:pathSplitterRange, normalizePath;
68         output = dirsJson;
69         foreach(p; pathSplitterRange(path.normalizePath))
70         {
71             JSONValue* currAddr = p in output;
72             if(currAddr is null)
73                 return false;
74             output = *currAddr;
75         }
76         return true;
77     }
78     bool exists(string path)
79     {
80         JSONValue dummy = void;
81         return getFromPath(path, dummy);
82     }
83     bool remove(string path){return false;}
84 
85     bool isDir(string path)
86     {
87         JSONValue temp = void;
88         if(!getFromPath(path, temp))
89             return false;
90         return temp.type == JSONType.object;
91     }
92 
93     ~this()
94     {
95         dirsJson.dispose();       
96     }
97 
98 }